import { axios } from '@/util/request'

//post
const postAction = function(url,parameter) {
  return axios({
    url: url,
    method:'post' ,
    data: parameter
  })
}

/**
 * 文件上传 用于富文本上传图片
 * @param url
 * @param parameter
 * @returns {*}
 */
export function uploadAction(url,parameter){
  return axios({
    url: url,
    data: parameter,
    method:'post' ,
    headers: {
      'Content-Type': 'multipart/form-data',  // 文件上传
    },
  })
}

//post method= {post | put}
const httpAction = function(url,parameter,method) {
  return axios({
    url: url,
    method:method ,
    data: parameter
  })
}

//put
const putAction = function(url,parameter) {
  return axios({
    url: url,
    method:'put',
    data: parameter
  })
}

//get
const getAction = function(url,parameter) {
  return axios({
    url: url,
    method: 'get',
    params: parameter
  })
}

//deleteAction
const deleteAction = function(url,parameter) {
  return axios({
    url: url,
    method: 'delete',
    params: parameter
  })
}

//登录
const login = (params)=>postAction("/sys/mLogin",params);
//手机号登陆
const phoneNoLogin = (params)=>postAction("/sys/phoneLogin",params);

const oaPhoneLogin = (params)=>postAction("/sys/oaPhoneLogin",params);
//获取验证码
const getSmsCaptcha = (params)=>postAction('/account/sms',params)
//登出
const logout = function(token){
  return axios({
    url: '/sys/logout',
    method: 'post',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      'X-Access-Token':  token
    }
  })
}

//字典标签专用（通过code获取字典数组）
const ajaxGetDictItems = (code, params)=>getAction(`/sys/dict/getDictItems/${code}`,params);
// 重复校验
const duplicateCheck = (params)=>getAction("/sys/duplicate/check",params);

/**
 * 获取文件访问路径
 * @param avatar
 * @param subStr
 * @returns {*}
 */
const getFileAccessHttpUrl = function(avatar,subStr){
    if(!subStr) subStr = 'http'
    if(avatar && avatar.startsWith(subStr)){
        return avatar;
    }else{
        return window._CONFIG['staticDomainURL'] + "/" + avatar;
    }
};

export {
  postAction,
  getAction,
  putAction,
  deleteAction,
  httpAction,

  login,
  oaPhoneLogin,
  phoneNoLogin,
  getSmsCaptcha,
  logout,

  ajaxGetDictItems,
  duplicateCheck,
  getFileAccessHttpUrl
}
